/*
 * @Author: sy
 * @Date: 2021-12-30 08:38:44
 * @LastEditTime: 2022-08-18 19:46:44
 * @Description: file content
 */
import storage from '@/utils/storage'
import request from '@/plugins/axios'
import { ddmgConfig, evnConfig } from '@/utils/appConfig'
import util from '@/libs/util.js'
import { EncodeBase64 } from '@/utils/encryption'

// export const startApiReq = ({ data = {}, callmethod }, otherParams) => {
//   const userInfo = storage.getStorage("userInfo");
//   const headers = {
//     callmethod,
//     hostid: ddmgConfig.host,
//     caller: ddmgConfig.caller,
//     userInfo,
//   }
//   return request({
//     url: '/api',
//     method: "post",
//     headers,
//     data,
//     ...otherParams
//   });
// };

export const startApiReq = ({ data = {}, callmethod }, timeout = 15000) => {
  console.log(data,callmethod)
  const token = util.cookies.get('token')
  if (callmethod !== 'api900103' && token && token !== 'undefined') {
    // 用户有做操作，请求接口不为心跳，token自动延期2小时
    const currentTime = new Date().getTime()
    const expiresTime = new Date(currentTime + 1000 * 60 * 120)
    util.cookies.set('token', token, { expires: expiresTime })
  }
  // if(callmethod == 'cos003111'){
  //   timeout = 120000
  // }
  const customdata = JSON.stringify({
    systemid: storage.getStorage('currentSystemID')
  })
  const headers = {
    callmethod,
    hostid: ddmgConfig.host,
    caller: ddmgConfig.caller,
    customdata: EncodeBase64(customdata)
  }
  // console.log('headers===', headers)
  // 由于mock.js是根据url请求路径来拦截请求，但微服务网关api的请求url都一样，是根据callmethod来区分接口
  // 因此，若为mock的时候，将callmethod拼接在真实的url后面，来给mock区分接口请求；
  let url = ddmgConfig.apiurl
  if (evnConfig.environment !== 0) {
    // && mockCodes.indexOf(callmethod) > -1
    // 正式环境不给mock  且 需要mock的接口才走mock
    url = evnConfig.isMock
      ? ddmgConfig.apiurl + '/' + callmethod
      : ddmgConfig.apiurl
  }
  // console.log('startApiReq url=', url)
  return request({
    url: url,
    method: 'post',
    headers,
    data,
    timeout
  })
}
